/*
 * 登录页面
 * 
 * */
var layer = require('layer');
var template = require('template');
var login = require('../../views/login');
var request = require('request');
var func = require('{pub}/function');
var base64 = require('{polyfill}/base64');

return function(){
    var _layer = layer({
        content:template.render.call(login, login.main),
        scrollbar:false,
        padding:20,
        width:382,
        height:422,
        title:{
            enable:false,
            text:'标题',
        },
        cancel:{
            enable:false,
            text:'关闭'
        },
        events:{
            'keyup:change .g-input input':'inChg',
            'focus .g-input input':'onFocus',
            'blur .g-input input':'onBlur validate',
            'click .g-input .inpclear':'inpClear',
            'click .g-input .eye':'cover',
            'click .g-sub .ui-button':'sub'
        },
        inChg:function(e, elem){///**控制手机号码 以及显示隐藏清除按钮和密码显示按钮**/
            var _this = elem,
            _len = _this.val().length;
            if(_len > 0){
                _this.parents('.g-input').children('.inpclear').removeClass('none');
                if(_this.attr('name') == 'password'){
                    _this.parents('.g-input').children('.eye').removeClass('none');
                }else if(_this.attr('name') == 'mobile' && e.keyCode != '37' && e.keyCode != '38' && e.keyCode != '39' && e.keyCode != '40'){
                    var _val = _this.val().replace(/\s/g, ""),
                        _lens = _val.length;
                        _nueval = '';
                    if(_lens <= 3){
                        _nueval = _val;
                    }
                    if(_lens > 3){
                        _nueval = _val.substring(0,3) + ' ' + _val.substring(3);
                    }
                    if(_lens > 7){
                        _nueval = _val.substring(0,3) + ' ' + _val.substring(3,7) + ' ' + _val.substring(7);
                    }
                    if(_lens >= 11){//手机号码获取去验证是否注册
                        _nueval = _val.substring(0,3) + ' ' + _val.substring(3,7) + ' ' + _val.substring(7,11);
                    }
                    _this.val(_nueval);
                }
            }else{
                _this.parents('.g-input').children('.inpclear').addClass('none');
                if(_this.attr('name') == 'password'){
                    _this.parents('.g-input').children('.eye').addClass('none');
                }
            }
        },
        onFocus:function(e, elem){//获得焦点
            var _this = elem;
            _this.removeClass('s-err').parents('.g-input').children('.ui-err').html('');
            if(_this.attr('name') == 'password'){
                if(!_this.parents('.g-input').children('.eye').hasClass('eyeshow')){
                    if(!_this.hasClass('onie8')){
                        _this.attr('type','password');
                    }
                }
            }
        },
        onBlur:function(e,elem){//失去焦点
            var inname = elem.attr('name');
            return inname
        },
        validate:function(e,elem,inname){//验证
            var fig = false;
            if(vali[inname].selector == 'name'){
                var _val = this.element.find('.login [name='+inname+']').val();
                if(inname == 'mobile'){
                    _val = _val.replace(/\s/g, "")
                }
                if(vali[inname].mode == 'plain'){
                    _val.length > 0?fig = true:fig = false;
                }else{
                    vali[inname].regular.test(_val)?fig = true:fig = false;
                }
            }
            if(!fig){
                this.element.find('.login [name='+inname+']').addClass('s-err');
                this.element.find('.login [name='+inname+']').parents('.g-input').children('.ui-err').html(this.element.find('.login [name='+inname+']').attr('data-msg-required'));
            }else{
                this.element.find('.login [name='+inname+']').parents('.g-input').children('.ui-err').html('');
            }
            return fig;
        },
        inpClear:function(e,elem){//清空
            var _this = elem;
            _this.addClass('none').parents('.g-input').find('input').val('').focus();
            if(_this.parents('.g-input').find('input').attr('name') == 'password'){
                _this.siblings('.eye').addClass('none');
            }
        },
        cover:function(e,elem){//显示密码和变***
            var _this = elem;
            if(_this.hasClass('eyeshow')){
                _this.removeClass('eyeshow').html('&#xe659;');
                if(_this.parents('.g-input').find('input').hasClass('onie8')){
                    var _html = _this.siblings('strong').html().replace('type=text','type=password');
                    _this.siblings('strong').html(_html);
                }else{
                    _this.parents('.g-input').find('input').attr('type','password');
                }
                
            }else{
                _this.addClass('eyeshow').html('&#xe65a;');
                if(_this.parents('.g-input').find('input').hasClass('onie8')){
                    var _html = _this.siblings('strong').html().replace('type=password','type=text').replace('value="','value="'+_this.parents('.g-input').find('input').val());
                    _this.siblings('strong').html(_html);
                }else{
                    _this.parents('.g-input').find('input').attr('type','text');
                }
            }
        },
        sub:function(e,elem){
            var methat = this.element;
            var inname = '',
                x = 0,
                y = 0;
            for(var i in vali){
                inname = i;
                x++;
                if(validate(inname)){
                    y++;
                }
            }
            if(x == y){
                var username = methat.find('.login [name=mobile]').val().replace(/\s/g, ""),
                    password = base64.encode(methat.find('.login [name=password]').val());
                request.get('user/login', {
                    'username':username,
                    'password':password
                }, {
                    200:function(res){
                        func.location('list');
                    },
                    other:function(res){
                        methat.find('.login [name=mobile]').focus();
                    }
                }, '正在登陆...', _layer)
            }
        },
        ccc:function(e, elem, data){
            // this.element.find('.login')
        }
    });
    var vali = {//验证
        mobile:{
            selector:'name',//选择器
            mode:'regular',//验证方式 plain表示普通（非空），regular表示正则
            regular:/^1[34578]\d{9}$/
        },
        password:{
            selector:'name',
            mode:'plain'
        },
    };
    $('.login').keyup(function(event){
        if(event.keyCode ==13){
            $(".g-sub .ui-button").trigger("click");
        }
    });

    function validate(inname){
        var fig = false;
        if(vali[inname].selector == 'name'){
            var _val = $('.login [name='+inname+']').val();
            if(inname == 'mobile'){
                _val = _val.replace(/\s/g, "")
            }
            if(vali[inname].mode == 'plain'){
                _val.length > 0?fig = true:fig = false;
            }else{
                vali[inname].regular.test(_val)?fig = true:fig = false;
            }
        }
        if(!fig){
            $('.login [name='+inname+']').addClass('s-err');
            $('.login [name='+inname+']').parents('.g-input').children('.ui-err').html($('.login [name='+inname+']').attr('data-msg-required'));
        }else{
            $('.login [name='+inname+']').parents('.g-input').children('.ui-err').html('');
        }
        return fig;
    }
}